package lab;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShareLinkServlet extends HttpServlet {

    private final Properties mailProperties = new Properties();

    @Override
    public void init(ServletConfig config) throws ServletException {
        mailProperties.setProperty("mail.transport.protocol", "smtp");
        mailProperties.setProperty("mail.host", "swen90002-22.cis.unimelb.edu.au");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final Session session = Session.getDefaultInstance(mailProperties);
        final MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress("from@no-reply.com"));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("email")));
            message.setSubject("Server URL");
            message.setText("http://swen90002-22.cis.unimelb.edu.au:8080/lab5/index.html");
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
